<template>
    <el-autocomplete v-model="infoName"
                     valueKey="name"
                     @blur="checkCurrentId"
                     :trigger-on-focus="true"
                     :fetch-suggestions="querySearchAsync"
                     @mouseenter.native="isMouseEnter=true"
                     @mouseleave.native="isMouseEnter=false"
                     :disabled="disabled"
                     :placeholder="placeholder" @select="handleSelectTer">
        <i class="el-input__icon" :class="infoName&&isMouseEnter&&!disabled?'el-icon-error':'el-icon-edit'" @click="handleClear"
           slot="suffix"></i>
        <template slot-scope="props">
            <div class="name">{{ props.item.resourceName }}</div>
        </template>
    </el-autocomplete>
</template>

<script>
    import CommonSearchInput from './common'
    import { getJobTerminalList } from "@/api/baseResource/baseResource";

    export default {
        model: {
            prop: 'name',
            event: 'selectName'
        },
        props: {
            name: {
                type: String,
                default: ''
            },
            id: {
                // type: Number
                default: 0
            },
            placeholder: {
                type: String,
                default: ''
            },
            iDisplayLength: {
                default: 65535
            },
            deptId: '',
            disabled:{
                type:Boolean,
                default:false,
            }
        },
        data() {
            return {
                infoName: this.name,
                isMouseEnter:false,
            }
        },
        methods: {
            // 异步搜索信息
            querySearchAsync(queryString, cb) {
                let params = {
                    search_LIKE_resourceName: queryString,
                    'iDisplayLength': this.iDisplayLength
                }
                if (this.deptId) {
                    params.search_EQ_deptId = this.deptId
                }
                getJobTerminalList(params).then((reponse) => {
                    cb(reponse.data.aaData)
                })
            },
            handleClear() {
                if (this.infoName&&!this.disabled) {
                    this.infoName = ''
                    this.$emit('select', '')
                    this.$emit('selectName', '')
                    this.$emit('selectId', '')
                }
            },
            handleSelectTer(item) {
                this.$emit('matchRecord', Object.assign({}, item))
                this.$emit('select', item.resourceCode)
                this.$emit('selectName', item.resourceName)
                this.$emit('selectId', item.id)
                this.infoName = item.resourceName
            },
            checkCurrentId() {
                if (!this.name) {
                    this.infoName = ''
                }
            }
        },
        watch: {
            name(newVal) {
                console.log(newVal)
                this.infoName = newVal
            },
        },
    }
</script>
